/* 
 * Copyright(c) 2005 Center for E-Commerce Infrastructure Development, The
 * University of Hong Kong (HKU). All Rights Reserved.
 *
 * This software is licensed under the GNU GENERAL PUBLIC LICENSE Version 2.0 [1]
 * 
 * [1] http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */

package com.gamex.findit.util;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;

public class Generator {
	
    private static int nameCounter = 0;
    private static Object nameCounterLock = new Object();
    public final static String SHA256 = "SHA256";
   
    private Generator() {}
    
    /**
     * 파일명 생성
     * @return
     */
    public static String generateFileName() {
        DataFormatter messageIdFormatter = DataFormatter.getInstance();
        Date timestamp = new Date();
        int localCounter;
        synchronized (nameCounterLock) {
            nameCounter %= 100;
            localCounter = nameCounter;
            nameCounter++;
        }

        final StringBuilder fileName = new StringBuilder();
        fileName.append(messageIdFormatter.formatDate(timestamp,"yyyyMMdd-HHmmss-SSS"));
        fileName.append(lpad(String.valueOf(localCounter), '0', 2));
       // fileName.append("-").append(generateUUID());

        return fileName.toString();
    }
    
    public static String generateUUID() {
        return UUID.randomUUID().toString();
    }
    
    private static String lpad(String s, char c, int len) {
        while (s != null && s.length() < len) {
            s = c + s;
        }
        return s;
    }
    
    public static byte[] getHash(String algType, InputStream is) throws IOException, NoSuchAlgorithmException {
		MessageDigest md;
		md = MessageDigest.getInstance(algType);
		byte b[] = new byte[512];
		for (int n = 0; (n = is.read(b, 0, b.length)) != -1;) {
			md.update(b, 0, n);
		}
		is.close();
		return md.digest();
	}
	
	public static byte[] getHash(String algType, byte[] b) 
			throws IOException, NoSuchAlgorithmException {
		MessageDigest md;
		md = MessageDigest.getInstance(algType);
		md.update(b, 0, b.length);
		return md.digest();
	}
	
    public static byte[] getHash(String algType, File file)
			throws IOException, NoSuchAlgorithmException {
		return getHash(algType, new BufferedInputStream(new FileInputStream(file)));
	}
    
    public static String getBase64EncodedHash(String algType, byte[] byteArray) throws IOException, NoSuchAlgorithmException {
	    MessageDigest md = MessageDigest.getInstance(algType);
        md.update(byteArray);
	    byte raw[] = md.digest();
	    return Base64.encodeBase64String(raw);
	}

    public static String getBase64EncodedHash(String algType, String plainText) throws IOException, NoSuchAlgorithmException {
    	return getBase64EncodedHash(algType, plainText.getBytes("UTF-8"));
	}
    
    public static String getBase64EncodedHash(String algType, InputStream is) throws IOException, NoSuchAlgorithmException {
	    return Base64.encodeBase64String(getHash(algType, is));
	}

    public static String getBase64EncodedHash(InputStream is) {
    	try {
    		return Base64.encodeBase64String(getHash(SHA256, is));
    	}
    	catch(Exception e) {
    		throw new RuntimeException(e.getMessage(), e);
    	}
	}

    public static String getBase64EncodedHash(byte[] byteArray) {
    	try {
    		return Base64.encodeBase64String(getHash(SHA256, byteArray));
    	}
    	catch(Exception e) {
    		throw new RuntimeException(e.getMessage(), e);
    	}
	}
}
